package com.itheima.pinda.controller.truck;


import com.itheima.pinda.DTO.entity.dto.PdTruckTypeDTO;
import com.itheima.pinda.DTO.entity.vo.Results;
import com.itheima.pinda.DTO.entity.vo.StatusCode;
import com.itheima.pinda.feign.service.truck.PdTruckTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 车辆类型表 前端控制器
 * </p>
 *
 * @author xy
 * @since 2021-11-11
 */
@RestController
@RequestMapping("/base/pdTruckType")
public class PdTruckTypeController {

    @Autowired
    private PdTruckTypeService truckTypeService;

    /**
     * 查询所有的车型
     * @Return
     */
    @PostMapping("/")
    public Results<List<PdTruckTypeDTO>> findTruckAll(){
        List<PdTruckTypeDTO> pdTruckTypes = truckTypeService.findTruckAll();
        if (ObjectUtils.isEmpty(pdTruckTypes)){
            return new Results<>(false, StatusCode.ERROR,"查询失败，请联系管理员",null);
        }
        return new Results<>(true,StatusCode.OK,"查询成功",pdTruckTypes);
    }

    /**
     * 高级查询
     * 查询特定的的车型
     * @Return
     */
    @PostMapping("/findTruck")
    public Results<List<PdTruckTypeDTO>> findTruckAdvance(
            String name,
            String allowableLoad,
            String allowableVolume){

//        System.out.println(name+"...");

        List<PdTruckTypeDTO> list = truckTypeService.findAdvance(name, allowableLoad, allowableVolume);
        if (ObjectUtils.isEmpty(list)){
            return new Results<>(false, StatusCode.ERROR,"查询失败，请联系管理员",null);
        }
        return new Results<>(true,StatusCode.OK,"查询成功",list);
    }
    /**
     *添加车型
     * @param dto
     * @return
     */
    @PostMapping("/addtruck")
    public Results addTruck(@RequestBody PdTruckTypeDTO dto){

        boolean truck = truckTypeService.addTruck(dto);
        if (truck){
        return new Results(true,StatusCode.OK,"添加成功",null);
        }
        return new Results(false,StatusCode.ERROR,"添加失败",null);
    }
    /**
     * 修改车型
     * @param dto
     * @return
     */
    @PostMapping("/updateTruck")
    public Results updateTruck(@RequestBody PdTruckTypeDTO dto){
        List<String > goodsTypeId = dto.getGoodsTypeId();
        boolean truck = truckTypeService.updateTruck(dto ,goodsTypeId);
        if (truck){
            return new Results(true,StatusCode.OK,"修改成功",null);
        }
        return new Results(false,StatusCode.ERROR,"修改失败",null);
    }
    /**
     * 删除车型(状态修改)
     * @param id
     * @return
     */
    @GetMapping("/deletetruck/{id}")
    public Results deletetruck ( @PathVariable( name = "id" ) String id){
        boolean truck = truckTypeService.deleteUpd(id);
        if (truck){
            return new Results(true,StatusCode.OK,"删除成功",null);
        }else {
            return new Results(false,StatusCode.ERROR,"删除失败",null);
        }
    }

    /**
     * 删除车型(彻底删除)
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public Results deletetruckreal(@PathVariable Integer id){
        boolean truck = truckTypeService.deleteReal(id);
        if (truck){
            return new Results(true,StatusCode.OK,"删除成功",null);
        }
        return new Results(false,StatusCode.ERROR,"删除失败",null);
    }
}
